﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using weikebuluo.DBUtility;
using weikebuluo.Controllers.Cs.Customer;
using NVelocity.Context;
using NVelocity;

namespace weikebuluo.web.wechat.website
{
    /// <summary>
    /// Home1 的摘要说明
    /// </summary>
    public class Home1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string _mid = context.Request.QueryString["userid"];
            string _openid = context.Request.QueryString["wxid"];
            if (!string.IsNullOrEmpty(_mid))
            {
                SqlParameter[] para = new SqlParameter[] { 
                    new SqlParameter("@mid",_mid)
                };
                //企业信息 
                DataTable dtMerchant = DbHelperSQL.Query("Select MId,[MName],[CopyRight],Template,Description,KeyWords,HomeShareImg,MPhone,HomeBgImg,FontColor from t_set_view where user_id=@mid", para).Tables[0];
                if (dtMerchant.Rows.Count > 0)
                {
                    DataRow drMerchant = dtMerchant.Rows[0];

                    DataSet dsHome = DbHelperSQL.Query(@"Select PicUrl,Href From t_home where user_id=@mid and Status=1;
                            select Id,Name,PicUrl,Href,Type,BgColor,Opacity,Color,BgImg,Name_Status,PicUrl_Status,BgImg_Status From t_icon where user_id=@mid and Status=1 order by case when sort is null then 1 else 0 end,sort;
                            select PicUrl,BgColor From t_menu where Type='Button' and user_id=@mid and Status=1;
                            select PicUrl,BgColor,LinkUrl From t_menu where Type='Menu' and user_id=@mid and Status=1
                            ", para);
                    DataTable dtHomeAd = dsHome.Tables[0];
                    DataTable dtHomeIcon = dsHome.Tables[1];
                    DataTable dtShortcutButtom = dsHome.Tables[2];
                    DataTable dtShortcutMenu = dsHome.Tables[3];

                    string _homeCss = "/web/wechat/website/" + (string.IsNullOrEmpty(drMerchant["Template"].ToString()) ? "Default" : drMerchant["Template"].ToString()) + "/Css/Home.css";//首页Css

                    IContext template = new VelocityContext();
                    //传递参数
                    template.Put("merchant", drMerchant);
                    template.Put("ad", dtHomeAd);//幻灯片广告
                    template.Put("icon", dtHomeIcon);//图标按钮
                    template.Put("button", dtShortcutButtom);//快捷菜单按钮
                    template.Put("menu", dtShortcutMenu);//快捷菜单
                    template.Put("homecss", _homeCss);//首页样式
                    BaseTemp bt = new BaseTemp();
                    context.Response.Write(bt.WriterTemplate("/web/wechat/website/loushu/weiweb/52/index.html", template, 0));
                }
                else
                {
                    context.Response.Write("参数不正确");
                }
            }
            else
            {
                context.Response.Write("参数不正确");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}